/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package store.forms;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

/**
 *
 * @author admin
 */
public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            mainLogin();
        });
    }
    
    public static void showClientMenu(String skey) {
        JFrame window = new JFrame("Store Administration");
        ClientMenu cm = new ClientMenu(skey);

        Timer checkRequest = new Timer(5000, (e) -> {
            String note = retrieveNote(skey);
            switch (note) {
            case "No notifiations.":
                break;
            case "Session key invalid...":
                break;
            default:
                JOptionPane.showMessageDialog(window, note);
            }
        });
        String[] tokens = getStoreInfo(skey).split(",");
        cm.setStoreName(tokens[0]);
        cm.setStoreContact(tokens[1]);
        cm.setStoreLoc(tokens[2]);
        window.setSize(720, 360);
        window.add(cm, BorderLayout.CENTER);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
        checkRequest.start();
    }
    
    public static void mainLogin() {
        JFrame window = new JFrame("Login");
        Login login = new Login();
        JPanel controls = new JPanel();
        ((FlowLayout) controls.getLayout()).setAlignment(FlowLayout.LEADING);
        JButton register = new JButton("Register");
        register.addActionListener((e) -> {
            JFrame w = new JFrame("Register User");
            JPanel south = new JPanel();
            w.setSize(400, 360);
            w.setLayout(new java.awt.BorderLayout());
            Register panel = new Register();
            w.add(panel, java.awt.BorderLayout.CENTER);
            JButton action = new JButton("Send");
            JButton close = new JButton("Close");
            close.addActionListener((evt) -> {
               w.dispose();
            });
            action.addActionListener((evt) -> {
                JOptionPane.showMessageDialog(w, panel.registerUser(),
                        "Register User", JOptionPane.INFORMATION_MESSAGE);
            });
            south.add(action);
            south.add(close);
            w.add(south, java.awt.BorderLayout.SOUTH);
            w.setLocationRelativeTo(window);
            w.setVisible(true);
            w.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        });
        JButton ok = new JButton("Ok");
        JButton cancel = new JButton("Cancel");
        ok.addActionListener((e) -> {
            if (login.isInputValid()) {
                String skey = login.getSessionKey(); 
                showClientMenu(skey);
                window.dispose();
            } else {
                JOptionPane.showMessageDialog(login,
                        "Please check the login information provided.",
                        "Information", JOptionPane.INFORMATION_MESSAGE);
            }
        });
        cancel.addActionListener((e) -> {
            System.exit(0);
        });
        controls.add(ok);
        controls.add(register);
        controls.add(cancel);

        window.setSize(400, 360);
        window.add(login, BorderLayout.CENTER);
        window.add(controls, BorderLayout.SOUTH);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }

    private static String getStoreInfo(java.lang.String skey) {
        store.StoreWebService_Service service = new store.StoreWebService_Service();
        store.StoreWebService port = service.getStoreWebServicePort();
        return port.getStoreInfo(skey);
    }

    private static String retrieveNote(java.lang.String skey) {
        store.StoreWebService_Service service = new store.StoreWebService_Service();
        store.StoreWebService port = service.getStoreWebServicePort();
        return port.retrieveNote(skey);
    }
}
